package kuzha.runtime;

import java.util.Map;

import kuzha.KuzhaObject;

public class VariablePattern extends CasePattern {
	public final String name;
	
	private VariablePattern(String name) {
		if (name == null)
			throw new IllegalArgumentException();
		this.name = name;
	}
	
	@Override
	public boolean match(KuzhaObject value, Map<String, KuzhaObject> vars) {
		if (vars.containsKey(name)) {
			KuzhaObject v2 = vars.get(name);
			return v2 == null ? value == null : v2.equals(value);
		}
		else {
			vars.put(name, value);
			return true;
		}
	}
	
	public static VariablePattern valueOf(String name) {
		return new VariablePattern(name);
	}
}
